<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.mercari_db.php 247 2013-09-23 06:30:21Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 247 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-23 06:30:21 +0000 (Mon, 23 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class mercari_db {

	public $db = false;
	private $server = DB_SERVER;
	private $username = DB_SERVER_USERNAME;
	private $password = DB_SERVER_PASSWORD;
	private $database = DB_DATABASE;
	private $db_type = DB_TYPE;
	private $pconnect = false;
	private $insertid;

	function __construct() {
		include_once(DIR_FS_CATALOG.'includes/classes/adodb/adodb-exceptions.inc.php');
		include(DIR_FS_CATALOG.'includes/classes/adodb/adodb.inc.php');
		$ADODB_CACHE_DIR = (defined('SQL_CACHEDIR') ? SQL_CACHEDIR : 'cache');
		$ADODB_COUNTRECS = false;
		$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
		$this->pconnect = (USE_PCONNECT == 'true' ? true : false);

		$this->db_connect();
	}

	function __destruct() {
		$this->db_close();
	}

	function db_connect() {
		global $ADODB_FETCH_MODE, $ADODB_COUNTRECS;

		if($this->pconnect) {
			try {
				$this->db = ADONewConnection($this->db_type);
				if(MEMCACHE_USE == 'true' && class_exists('Memcache')) {
					$this->db->memCache = true;
					$this->db->memCacheHost = array(MEMCACHE_HOSTS);
					$this->db->memCachePort = MEMCACHE_PORT;
					$this->db->memCacheCompress = MEMCACHE_COMPRESS;
				}
				#$this->db->debug = true;
				$this->db->pConnect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
			} catch (Exception $e) {
				var_dump($e);
				adodb_backtrace($e->gettrace());
			}

		} else {
			try {
				$this->db = ADONewConnection($this->db_type);
				if(MEMCACHE_USE == 'true' && class_exists('Memcache')) {
					$this->db->memCache = true;
					$this->db->memCacheHost = array(MEMCACHE_HOSTS);
					$this->db->memCachePort = MEMCACHE_PORT;
					$this->db->memCacheCompress = MEMCACHE_COMPRESS;
				}
				#$this->db->debug = true;
				$this->db->Connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
			} catch (Exception $e) {
				var_dump($e);
				adodb_backtrace($e->gettrace());
			}

		}
		$this->db->Execute("SET NAMES 'utf8'");
		$this->db->Execute("SET CHARACTER_SET_CLIENT = utf8");
		$this->db->Execute("SET CHARACTER_SET_RESULTS = utf8");

		return $this->db;
	}

	function db_close() {
		return $this->db->Disconnect();
	}

	function db_query($query, $cache = false) {
		try {
			if(defined('DB_CACHE') && DB_CACHE == 'true' && $cache) {
				$this->db->cacheSecs = DB_CACHE_EXPIRE; 
				$rs = $this->db->CacheExecute($query);
			} else
				$rs = $this->db->Execute($query);
				
		} catch(Exception $e) {
			echo '<pre class="db_error">';
			print_r($e->msg);
			adodb_backtrace($e->gettrace());
			echo '</pre>';
		}
		return $rs;
	}

	function db_query_limit($query, $limit = - 1, $cache = false, $offset = - 1) {
		try {
			if(defined('DB_CACHE') && DB_CACHE == 'true' && $cache) {
				$this->db->cacheSecs = DB_CACHE_EXPIRE;
				$rs = $this->db->CacheSelectLimit($query, $limit, $offset);
			} else
				$rs = $this->db->SelectLimit($query, $limit, $offset);
				
		} catch(Exception $e) {
			echo '<pre class="db_error">';
			print_r($e->msg);
			adodb_backtrace($e->gettrace());
			echo '</pre>';
		}
		return $rs;
	}

	function db_fields($table) {
		return $this->db->MetaColumnNames($table);
	}

	function db_tables() {
		return $this->db->MetaTables();
	}

	function db_insert_id() {
		return $this->db->Insert_ID();
	}

	function db_prepare($string) {
		$rs = $this->db->qstr($string, get_magic_quotes_gpc());
		if(is_array($rs))
			return $result['0'];
		else
			return $rs;
	}

	function db_perform($table, $data, $action = 'insert', $parameters = '') {
		$action = strtolower($action);
	
		if ($action == 'insert') {
			$query = 'INSERT INTO '.$table.' (';
			foreach($data AS $columns => $value) {
				$value = (string)($value);
				if ($value !== "")
					$query.= $columns.', ';
			}
			$query = substr($query, 0, -2).') VALUES (';
			foreach($data as $key => $value) {
				$value = (string)($value);
				if ($value !== "") {
					switch ($value) {
					case 'now()':
					case 'NOW()':
						$query.= $this->db->sysTimeStamp.', ';
						break;

					case 'null':
					case 'NULL':
						$query.= 'null, ';
						break;

					default:
						if (!ctype_digit($value) || (ctype_digit($value) && substr($value, 0, 1) == '0'))
							$query.= $this->db_prepare($value).', ';
						else
							$query.= $value.', ';
						break;
					}
				}
			}
			$query = substr($query, 0, -2).')';

		} elseif ($action == 'update') {
			$query = 'UPDATE '.$table.' SET ';
			foreach($data as $columns => $value) {
				$value = (string)($value);
				switch ($value) {
				case 'now()':
				case 'NOW()':
					$query.= $columns.' = '.$this->db->sysTimeStamp.', ';
					break;

				case 'null':
					$query.= $columns.' = NULL, ';
					break;

				default:
					if (!ctype_digit($value) || (ctype_digit($value) && substr($value, 0, 1) == '0'))
						$query.= $columns.' = '.$this->db_prepare($value).', ';
					else
						$query.= $columns.' = '.$value.', ';
					break;
				}
			}
			$query = substr($query, 0, -2).' WHERE '.$parameters;
		}

		return $this->db_query($query);
	}

	function recreate_session() {
		adodb_session_regenerate_id();
	}

	function db_error($query, $errno, $error) {
		die('<b>'.$errno.' - '.$error.'<br /><br />'.$query.'<br /><br /><small>[SQL Error]</small><br /><br /></b>');
	}
}